﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using ServerBase.Config;
using UtilPublic.ServerLoger;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using ServerBase.Other;
using ServerBase.IniFile;
using static ServerBase.IniFile.IniConf;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using UtilPublic.ApiDispatcher;
using ServerBase.LunarSocket;

namespace ServerBase.Service
{
    // 命令行指令
    public class BaseCmds
    {
        #region 单例
        private static readonly BaseCmds _instance = new BaseCmds();
        public static BaseCmds Instance { get { return _instance; } }
        private BaseCmds() { } //私有构造方法(防止被外部实例化)
        #endregion

        [ApiDpCmd("h", "显示帮助")]
        public void CmdShowHelp(string cmd, string[] args)
        {
            foreach (var item in ApiDpCmdMgr.DictCmdApi.Values)
            {
                Util.Show($"\t{item.Att.Cmd}\t{item.Att.Desc}");
            }
        }
        [ApiDpCmd("conf", "重新读取配置文件")]
        public void CmdReadConfig(string cmd, string[] args)
        {
            Conf.InitConfSystem();
            Conf.ConfToolServerPlatformExt.TryGetValue(BaseServerInfo.ServerPlatform, out BaseServerInfo.CfgPlatform);
            Conf.ConfToolServerGame.TryGetValue(BaseServerInfo.ConfigId, out BaseServerInfo.Cfg);
            if (Conf.InitConf())
            {
                Util.Show("重新读取配置文件成功");
            }
            else
            {
                Util.Show("重新读取配置文件失败");
            }
        }
        [ApiDpCmd("ini", "重新读取INI配置文件")]
        public void CmdReadIni(string cmd, string[] args)
        {
            if (IniConf.InitIniConf())
            {
                Util.Show("重新读取INI配置文件成功");
            }
            else
            {
                Util.Show("重新读取INI配置文件失败");
            }
        }
        [ApiDpCmd("log", "显示和调整日志等级")]
        public void CmdLogerChanggeLevel(string cmd, string[] args)
        {
            ELogLevel OldLevel = loger.GetLevel();
            Util.Show($"当前日志等级为：{OldLevel}={(int)OldLevel}");
            if (args.Length < 2)
            {
                Util.Show("请输入日志等级(1-6) 例如: log 1");
                return;
            }
            int NewLevl = args[1].ToInt();
            ELogLevel Level = loger.ChangeLevel(NewLevl);
            Util.Show($"调整日志等级为：{Level}={(int)Level}");
        }
        [ApiDpCmd("lock", "登陆锁定")]
        public void CmdRestrictLoginLock(string cmd, string[] args)
        {
            BaseServerInfo.RestrictLogin = true;
            Util.Show($"登录锁定");
        }
        [ApiDpCmd("unlock", "登录解锁")]
        public void CmdRestrictLoginUnlock(string cmd, string[] args)
        {
            BaseServerInfo.RestrictLogin = false;
            Util.Show($"登录解锁");
        }
        [ApiDpCmd("info", "显示服务器信息")]
        public void CmdShowState(string cmd, string[] args)
        {
            Util.Show($"****************************************************************************************");
            Util.Show($"服务器配置ID：{BaseServerInfo.ConfigId}");
            Util.Show($"服务器地址：{BaseServerInfo.ServerIp}");
            Util.Show($"服务器端口：{BaseServerInfo.ServerPort}");
            Util.Show($"服务器对外地址：{BaseServerInfo.ServerIp}");
            Util.Show($"服务器连接数：{BaseAppServer.SessionCount}");
            Util.Show($"服务器运营平台：{BaseServerInfo.ServerPlatform}");

            Util.Show($"****************************************************************************************");
            Util.Show($"是否连接Mysql数据库：{BaseServerInfo.MysqlState}");
            Util.Show($"是否限制登录：{BaseServerInfo.RestrictLogin}");
            Util.Show($"单服限制人数：{BaseServerInfo.Cfg.RestrictOnline}");
            Util.Show($"游戏验证PIN码：{BaseServerInfo.CfgPlatform.VersionPin}");

            Util.Show($"****************************************************************************************");
        }
        [ApiDpCmd("dd", "打开或关闭调试开关")]
        public void 打开或关闭调试开关(string cmd, string[] args)
        {
            loger.临时调试开关 = !loger.临时调试开关;
            Util.Show($"临时调试开关 {loger.临时调试开关}");
        }
        public void 重新读取配置文件指令()
        {
            CmdReadConfig(null, null);
        }
        
    }

}